/**
 * FileName: L12
 * Author:   powersi
 * Date:     2021/3/31 15:26
 * Description:
 */
package com.zm.demo.core.leetcode;

/**
 * Description: 
 * Author:      powersi
 * Date:        2021/3/31 15:26
 */
public class L12 {


    public static void main(String[] args) {
        String r = new L12().intToRoman(5);
        System.out.println(r);
    }

    public String intToRoman(int num) {
        int [] a = {num, num%1000, num%100, num%10};
        int [] div = {1000,100,10,1};
        StringBuilder r = new StringBuilder();
        for (int i = 0; i < 4 ; i++) {
            int divResult = a[i] / div[i];
            if (i == 0) {
                for (int j = 0; j < divResult; j++) {
                    r.append("M");
                }
            } else if (i == 1) {
                calc(divResult, r, "CM", "D", "CD", "C");
            }else if (i == 2){
                calc(divResult, r, "XC", "L", "XL", "X");
            } else if (i == 3){
                calc(divResult, r, "IX", "V", "IV", "I");
            }
        }
        return r.toString();
    }

    public void calc(int divResult, StringBuilder r, String nine, String five, String four, String one){
        if (divResult == 9){
            r.append(nine);
        }else if (divResult>=5 && divResult < 9){
            r.append(five);
            for (int k = 5; k < divResult; k++) {
                r.append(one);
            }
        } else if (divResult == 4){
            r.append(four);
        } else if (divResult < 4 && divResult > 0){
            for (int i = 0; i < divResult ; i++) {
                r.append(one);
            }
        } else if (divResult == 0){
            return;
        }
    }
}
